var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var circleUrl = require("url/circle");
var http = require("helper/http");

var listview = ui("do_ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);
var noData = [{"template":1,"labelContent":"暂无话题"}];

/**********加载页头begin***********/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});
ui(addheader+".labelTitle").text = "我的圈子动态";
/**********加载页头end***********/
page.on("back", function() {
	app.closePage();
})

page.on("loaded", function(){
	getFirstPageData();
});

var pageNum = 1;
var httpUrl = circleUrl.url.topic.myTopicList;

//全部刷新数据
function getFirstPageData(){
	pageNum=1;
	http.post(httpUrl,{page:pageNum},function(data){
		listview.rebound();
		listdata.removeAll();//清除数据
		if(data.topicPage.content.length){
			listdata.addData(data.topicPage.content);
		}else{
			listdata.addData(noData);
		}
		listview.refreshItems();//刷新item数据
	});
}

function getNextPageData(){
	pageNum++;
	http.post(httpUrl,{page:pageNum},function(data){
		if(data.topicPage){
			listdata.addData(data.topicPage.content);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		getFirstPageData();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});

page.on("mytopic_deleteTopic", function(data) {
	for (var i = 0; i < listdata.getCount(); i++) {
		var oneData = listdata.getOne(i);
		if (oneData.id==data) {
			oneData.optUserId = oneData.creatorId;//本人删除
			oneData.status = 2;//已删除
			oneData.isShow = 2;//已屏蔽
			listdata.updateOne(i, oneData);
			listview.refreshItems();
			break;
		}
	}
});